package BackEnd;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

public class OptionManager {
	private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
	private String endBoerseTime;
	private String startBoerseTime;
	private Date startDate;
	private Date endDate;
	private static OptionManager instance = null;
	private String laf;
	private String server;
	private String verzeichnis;

	public OptionManager(){
		loadOptionsFromFile();
	}
	
	public void setDefaultOptions(){
		endBoerseTime = "17:30";
		startBoerseTime = "09:00";
		laf = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel";
		server = "rmi://rauch-systems.de/Server";
		parseDates();
	}
	
	private void parseDates(){
		try {
			startDate =  sdf.parse(startBoerseTime);
			endDate = sdf.parse(endBoerseTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
	

	public Date parseDate(Timestamp time){
		try {
			return sdf.parse(time.getHours() + ":" + time.getMinutes());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public void saveOptionsInFile(){
		//String verzeichnis = System.getProperty("user.dir");
		Properties p = new Properties();
		p.setProperty("endBoerseTime", this.getEndBoerseTime());
		p.setProperty("startBoerseTime", this.getStartBoerseTime());
		p.setProperty("Laf", this.getLaf());
		p.setProperty("Server", this.getServer());
		System.out.println(verzeichnis);
		File f = new File(verzeichnis + "\\\\Options.properties");
		try {
			p.storeToXML(new FileOutputStream(f), new Date(System.currentTimeMillis()).toString());
		} catch (FileNotFoundException e) {} 
		  catch (IOException e) {}
	}
	
	public void loadOptionsFromFile(){
		//String verzeichnis = System.getProperty("user.dir");
		File f = new File(verzeichnis + "\\\\Options.properties");
		System.out.println(verzeichnis);
		if (f.exists()){
		Properties p = new Properties();
		try {
			p.loadFromXML(new FileInputStream(f));
		} catch (InvalidPropertiesFormatException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.setEndBoerseTime(p.getProperty("endBoerseTime"));
		this.setStartBoerseTime(p.getProperty("startBoerseTime"));
		this.setLaf(p.getProperty("Laf"));
		this.setServer(p.getProperty("Server"));
		parseDates();
		} else
			setDefaultOptions();
	}
	
	
	public String getEndBoerseTime() {
		return endBoerseTime;
	}


	public void setEndBoerseTime(String endBoerseTime) {
		this.endBoerseTime = endBoerseTime;
		try {
			endDate = sdf.parse(endBoerseTime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	public String getStartBoerseTime() {
		return startBoerseTime;
	}


	public void setStartBoerseTime(String startBoerseTime) {
		this.startBoerseTime = startBoerseTime;
		try {
			startDate =  sdf.parse(startBoerseTime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	public Date getStartDate() {
		return startDate;
	}


	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}


	public Date getEndDate() {
		return endDate;
	}


	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}
	

	public String getLaf() {
		return laf;
	}

	public void setLaf(String laf) {
		this.laf = laf;
	}

	
	public static synchronized OptionManager getInstance(){
		if(instance == null){
			instance = new OptionManager();
		}
		return instance;
	}

	public String getServer() {
		return server;
	}

	public void setServer(String server) {
		this.server = server;
	}
	
	public void setVerzeichnis(String verzeichnis){
		this.verzeichnis = verzeichnis;
	}

}



